package com.thinkgem.jeesite.modules.sys.Scheduled;

import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.entity.customer.Customer;
import com.thinkgem.jeesite.modules.sys.entity.defaulttime.DefaultTime;
import com.thinkgem.jeesite.modules.sys.entity.firm.Firm;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.sys.service.customer.CustomerService;
import com.thinkgem.jeesite.modules.sys.service.defaulttime.DefaultTimeService;
import com.thinkgem.jeesite.modules.sys.service.firm.FirmService;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Lazy(false)
public class ScheduledFirmTask {
    @Resource
    SystemService systemService;
    @Resource
    FirmService firmService;
    @Resource
    CustomerService customerService;
    @Resource
    OfficeService officeService;
    @Resource
    DefaultTimeService defaultTimeService;

    @Scheduled(cron="0/10 * *  * * ? ")   //每10秒执行一次
    public void firmCreate(){
        List<Customer> list = customerService.getAll();
        for(Customer customer:list){
            if(customer.getDaoFlag().equals("0")){
                customer.setDaoFlag("1");
                customerService.save(customer);
                Firm firm = new Firm();
                User user = new User();Office office = new Office();
                user.setName(customer.getDqName());office.setId(customer.getDq());
                user.setOffice(office);

                firm.setKfId(systemService.getkfId(user));
                firm.setOffice(officeService.get(customer.getDq()));
                firm.setMember(customerService.get(customer.getId()));
                firm.setYwStatic("1");
                firm.setHfTime(new Date());
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                DefaultTime defaultTime = defaultTimeService.get("1");
                int num = Integer.valueOf(defaultTime.getHuiDay());
                c.add(Calendar.DAY_OF_MONTH, num);
                firm.setHfFtime(c.getTime());
                firm.setHui(String.valueOf(num));
                firm.setCreateBy(systemService.getUser("1"));
                firm.setUpdateBy(systemService.getUser("1"));
                firmService.save(firm);
            }
        }
    }


}
